package com.holyn.guofang.view.my;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.my.MyContract;
import com.holyn.guofang.presenter.my.MyPresenter;
import com.holyn.guofang.utils.MD5Uitl;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.viewcommon.dialog.ValidMethodDialog;
import com.holyn.guofang.viewcommon.radiobutton.DrawableCenterRadioButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/8/25.
 */

public class MyCardManagerUpdateRuleActivity extends BaseActivity implements BaseView, MyContract.UpdateValidMethodView{
    private static final int REQUEST_ACTION_POST_VALID_METHOD_CODE = 0x0001;
    private static final int REQUEST_ACTION_POST_UPDATE_VALID_METHOD = 0x0002;

    private static final String INTENT_KEY_CARD_ID = "INTENT_KEY_CARD_ID";
    private static final String INTENT_KEY_CARD_NUM = "INTENT_KEY_CARD_NUM";

    @BindView(R.id.tv_card_num)
    TextView tvCardNum;
    @BindView(R.id.tv_get_valid_code)
    TextView tvGetValidCode;
    @BindView(R.id.et_valid_code)
    EditText etValidCode;
    @BindView(R.id.rb_valid_password)
    DrawableCenterRadioButton rbValidPassword;
    @BindView(R.id.rb_valid_code)
    DrawableCenterRadioButton rbValidCode;
    @BindView(R.id.rb_valid_none)
    DrawableCenterRadioButton rbValidNone;
    @BindView(R.id.rg_valid_method)
    RadioGroup rgValidMethod;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    MyContract.Presenter presenter;

    Integer cardID;
    Integer validMethod = -1;
    String validCode;
    String password="";

    private String cardNum;

    private ValidMethodDialog validMethodDialog = null;

    public static void start(Activity activity, Integer cardID, String cardNum){
        Intent intent = new Intent(activity, MyCardManagerUpdateRuleActivity.class);
        intent.putExtra(INTENT_KEY_CARD_ID, cardID);
        intent.putExtra(INTENT_KEY_CARD_NUM, cardNum);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_card_manager_update_rule;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("修改规则");

        cardID = getIntent().getIntExtra(INTENT_KEY_CARD_ID, 0);
        cardNum = getIntent().getStringExtra(INTENT_KEY_CARD_NUM);
        tvCardNum.setText(cardNum);

        presenter = new MyPresenter(this);
        presenter.addUpdateValidMethodView(this);

        initradioGroupView();
    }

    private void initradioGroupView(){
        rgValidMethod.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb_valid_password:
                        showValidMethodDialog();
                        break;
                    case R.id.rb_valid_code:
                        validMethod = 1;
                        break;
                    case R.id.rb_valid_none:
                        validMethod = 2;
                        break;
                }
            }
        });
    }

    private void showValidMethodDialog(){
        if (validMethodDialog == null){
            validMethodDialog = new ValidMethodDialog(MyCardManagerUpdateRuleActivity.this
                    , new ValidMethodDialog.OnSureClickListener(){
                @Override
                public void onSureClick(String value) {
                    password = value;
                    validMethod = 0;
                }
            }
                    , new ValidMethodDialog.OnCancelClickListener(){
                @Override
                public void onCancelClick() {
                    rgValidMethod.clearCheck();
                    validMethod = -1;
                    password = "";
                }
            });
        }
        validMethodDialog.show();
    }

    @OnClick({R.id.tv_get_valid_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_valid_code:
                presenter.postValidMethodCode(REQUEST_ACTION_POST_VALID_METHOD_CODE);
                break;
            case R.id.btn_submit:
                validCode = etValidCode.getText().toString().trim();
                if (TextUtils.isEmpty(validCode)){
                    toastMsg("请输入验证码");
                    return;
                }
                if (validMethod == -1){
                    toastMsg("请选择扣费规则");
                    return;
                }
                presenter.postUpdateValidMethod(REQUEST_ACTION_POST_UPDATE_VALID_METHOD, cardID, validMethod, validCode, MD5Uitl.getMd5Value(password));
                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(int requestAction, StatusWo statusWo) {
        if (requestAction == REQUEST_ACTION_POST_VALID_METHOD_CODE){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("发送验证码成功");
            } else {
                toastMsg("发送验证码失败");
            }
        } else if (requestAction == REQUEST_ACTION_POST_UPDATE_VALID_METHOD){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("修改规则成功");
            } else {
                toastMsg("修改规则失败");
            }
        }
    }
}
